home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 1999 May / SGI IRIX 6.5 Applications 1999 May.iso / dist / arraysvcs.idb / usr / lib / array / aps.z / aps
Text File  |  1998-10-27  |  4KB  |  164 lines

  1. #!/usr/bin/perl
  2. #
  3. # Simple script to implement "array ps". 
  4. #
  5. # Usage: aps local <name>
  6. #    or: aps merge <file....>
  7. #    or: aps localonly [-Z]
  8. #
  9.  
  10. #
  11. # The local variant: this should be run on each node in the array
  12. #
  13. if ($ARGV[0] eq "local") {
  14.  
  15.     #
  16.     # Start up a "ps" command and trap its output, discarding the header
  17.     #
  18.     open(PS, "ps -ef |") || die "Couldn't run ps: $!\n";
  19.     <PS>;
  20.  
  21.     #
  22.     # Process each line of ps output
  23.     #
  24.     while (<PS>) {
  25.  
  26.     #
  27.     # Split out individual fields
  28.     #
  29.     ($fields1, $fields2) = unpack("A39 A*", $_);
  30.     ($uid, $pid, $ppid, $cpu, $stime, $tty) = split(" ", $fields1);
  31.     ($time, $command) = split(" ", $fields2, 2);
  32.  
  33.     #
  34.     # Determine the ASH of the current process
  35.     #
  36.     $ash = `ainfo ash -f 2 -i $pid`;
  37.     chop $ash;
  38.  
  39.     #
  40.     # Print the results neatly, tossing ASH "0" (which is normally
  41.     # just system processes)
  42.     #
  43.     printf "%18s %12s %5s %8s %7s %s\n",
  44.         $ash, $ARGV[1], $pid, $uid, $time, $command
  45.             unless $ash eq "0x0000000000000000"
  46.     }
  47. }
  48.  
  49. #
  50. # The merge variant: this is run only on one node using the output
  51. # from "aps local" on each node as its input files
  52. #
  53. elsif ($ARGV[0] eq "merge") {
  54.  
  55.     #
  56.     # Read all of the input files into a list
  57.     #
  58.     shift @ARGV;
  59.     @all = <ARGV>;
  60.  
  61.     #
  62.     # Sort the list using the "sortps" subroutine
  63.     #
  64.     @new = sort sortps @all;
  65.  
  66.     #
  67.     # Print a nice header line
  68.     #
  69.     print "        ASH              Host     PID     User    Time Command\n";
  70.     print "----------------------------------------------------------------\n";
  71.  
  72.     #
  73.     # Print every line except for those with no ASH (probably processes
  74.     # that terminated before the we could find its ASH)
  75.     #
  76.     foreach $line (@new) {
  77.     print $line unless $line =~ /UNAVAILABLE/;
  78.     }
  79. }
  80.  
  81. #
  82. # The localonly variant: essentially just a "ps" with ASHs, this would
  83. # be used by curious users or aview
  84. #
  85. elsif ($ARGV[0] eq "localonly") {
  86.  
  87.     #
  88.     # Start up a "ps" command and trap its output, discarding the header
  89.     #
  90.     open(PS, "ps -ef |") || die "Couldn't run ps: $!\n";
  91.     <PS>;
  92.  
  93.     #
  94.     # Process each line of ps output
  95.     #
  96.     $i = 0;
  97.     while (<PS>) {
  98.  
  99.     #
  100.     # Split out individual fields
  101.     #
  102.     ($fields1, $fields2) = unpack("A39 A*", $_);
  103.     ($uid, $pid, $ppid, $cpu, $stime, $tty) = split(" ", $fields1);
  104.     ($time, $command) = split(" ", $fields2, 2);
  105.  
  106.     #
  107.     # Determine the ASH of the current process
  108.     #
  109.     $ash = `ainfo ash -f 2 -i $pid`;
  110.     chop $ash;
  111.  
  112.     #
  113.     # Print the results neatly, tossing ASH "0" (which is normally
  114.     # just system processes) and "UNAVAILABLE" (meaning the process
  115.     # probably went away)
  116.     #
  117.     if ($ash ne "UNAVAILABLE"  &&
  118.         ($ARGV[1] ne "-Z"  ||  $ash ne "0x0000000000000000"))
  119.     {
  120.         $new[$i] = sprintf("%18s %5s %8s %s\n", $ash, $pid, $uid, $command);
  121.         $i = $i + 1;
  122.     }
  123.     }
  124.  
  125.     #
  126.     # Sort the list using the "sortlocalonly" subroutine
  127.     #
  128.     @sorted = sort sortlocalonly @new;
  129.  
  130.     #
  131.     # Print a header
  132.     #
  133.     print "        ASH          PID     USER COMMAND\n";
  134.  
  135.     #
  136.     # Dump the results
  137.     #
  138.     foreach $line (@sorted) {
  139.     print $line;
  140.     }
  141. }
  142.  
  143.  
  144. #
  145. # Sort function: sorts "aps local" lines first by ASH, then hostname, then PID
  146. #
  147. sub sortps {
  148.     ($Aash, $Ahost, $Apid) = split(" ", $a);
  149.     ($Bash, $Bhost, $Bpid) = split(" ", $b);
  150.  
  151.     (($Aash cmp $Bash) || ($Ahost cmp $Bhost) || ($Apid <=> $Bpid));
  152. }
  153.  
  154.  
  155. #
  156. # Sort function: sorts "aps localonly" lines first by ASH, then PID
  157. #
  158. sub sortlocalonly {
  159.     ($Aash, $Apid) = split(" ", $a);
  160.     ($Bash, $Bpid) = split(" ", $b);
  161.  
  162.     (($Bash cmp $Aash) || ($Bpid <=> $Apid));
  163. }
  164.